/**
 * 
 */
package com.cap.cfae;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cap.cfae.cfae.CfaeUserPubsenView;
import com.eos.das.entity.criteria.CriteriaType;
import com.eos.das.entity.criteria.ExprType;
import com.eos.das.entity.criteria.ExprType.OP;
import com.eos.foundation.data.DataObjectUtil;
import com.eos.foundation.database.DatabaseUtil;
import com.eos.system.annotation.Bizlet;
import com.kp.persistance.types.Date;

import commonj.sdo.DataObject;

/**
 * @author Administrator
 * @date 2018-03-07 20:58:39
 * 
 */
@Bizlet("")
public class PubsenService {
	/**
	 * 获取列表
	 * 
	 * @param token
	 * @param user
	 * @param searchword
	 * @param firstclass
	 * @param subclass
	 * @param startdate
	 * @param enddate
	 * @param ismyclloection
	 * @param ismycare
	 * @param isreaded
	 * @param currentPage
	 * @param pagesize
	 * @return
	 * @throws ParseException
	 */
	@Bizlet("getPubsens")
	public Map<String, Object> getPubsens(String token, String user,
			String searchword, String firstclass, String subclass,
			String startdate, String enddate, String ismyclloection,
			String ismycare, String isreaded, String currentPage,
			String pagesize) throws ParseException {
		Map<String, Object> datas = new HashMap<String, Object>();
		CriteriaType criteria = CriteriaType.FACTORY.create();
		criteria.set_entity(CfaeUserPubsenView.QNAME);
		List<ExprType> exprList = new ArrayList<ExprType>();

		// 查找对应用户的记录
		ExprType exprUser = ExprType.FACTORY.create();
		exprUser.set_opEnum(OP.EQ);
		exprUser.set_property("userid");
		exprUser.set_value(user);
		exprList.add(exprUser);

		// 搜索
		// if (searchword != null && !"".equals(searchword)) {
		// ExprType exprSearch = ExprType.FACTORY.create();
		// exprSearch.set_opEnum(OP.LIKE);
		// exprSearch.set_property("cfaePubsenKeywords");
		// exprSearch.set_value(searchword);
		// exprList.add(exprSearch);
		// }

		// 一级类别
		if (firstclass != null && !"".equals(firstclass)) {
			ExprType exprGrade = ExprType.FACTORY.create();
			exprGrade.set_opEnum(OP.EQ);
			exprGrade.set_property("cfaePubsenFirstclass");
			exprGrade.set_value(firstclass);
			exprList.add(exprGrade);
		}

		// 时间范围
		if (startdate != null && !"".equals(startdate) && enddate != null
				&& !"".equals(enddate)) {
			SimpleDateFormat sdF = new SimpleDateFormat("yyyyMMdd");
			Date startdateDate = (Date) sdF.parse(startdate);
			Date endDate2 = (Date) sdF.parse(enddate);
			// ExprType exprStartDate = ExprType.FACTORY.create();
			// exprStartDate.set_opEnum(OP.GE);
			// exprStartDate.set_property("cfaePubsenDate");
			// exprStartDate.set_value(startdate);
			// ExprType exprEndDate = ExprType.FACTORY.create();
			// exprStartDate.set_opEnum(OP.LE);
			// exprStartDate.set_property("cfaePubsenDate");
			// exprStartDate.set_value(enddate);
			//
			// exprList.add(exprStartDate);
			// exprList.add(exprEndDate);
		}
		// 是否我的收藏
		if (ismyclloection != null && !"".equals(ismyclloection)) {
			ExprType exprMYCollection = ExprType.FACTORY.create();
			exprMYCollection.set_opEnum(OP.EQ);
			exprMYCollection.set_property("iscollected");
			exprMYCollection.set_value("1");
			exprList.add(exprMYCollection);
		}

		// 是否我的关注
		if (ismycare != null && !"".equals(ismycare)) {
			ExprType exprMycare = ExprType.FACTORY.create();
			exprMycare.set_opEnum(OP.EQ);
			exprMycare.set_property("iscare");
			exprMycare.set_value("1");
			exprList.add(exprMycare);
		}
		// 是否已读
		if (isreaded != null && !"A".equals(isreaded)) {
			ExprType exprisReaded = ExprType.FACTORY.create();
			exprisReaded.set_opEnum(OP.EQ);
			exprisReaded.set_property("ismyreaded");
			if ("y".equals(isreaded)) {
				exprisReaded.set_value("y");
			} else {
				exprisReaded.set_value("n");
			}
			exprList.add(exprisReaded);
		}

		int currpage = Integer.parseInt(currentPage);
		int size = Integer.parseInt(pagesize);

		criteria.set_expr(exprList);
		DataObject[] objectArray = DatabaseUtil.queryEntitiesByCriteriaEntity(
				"default", criteria, (currpage - 1) * size, size);
		CfaeUserPubsenView pubsenView = (CfaeUserPubsenView) DataObjectUtil
				.createDataObject("com.cap.cfae.cfae.CfaeUserPubsenView");

		// 如何统计总数,包括时间区间。。。

		Integer total = DatabaseUtil.countByTemplate("default", pubsenView);
		// Integer total = DatabaseUtil.count(arg0, arg1)
		List<Map<String, Object>> pubsenList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < objectArray.length; i++) {
			Map<String, Object> pubsenObject;
			pubsenObject = new HashMap<String, Object>();
			DataObject dataObject = objectArray[i];
			List<Object> properties = dataObject.getInstanceProperties();
			for (Object property : properties) {
				String pString = property.toString();
				String keyString = pString.substring(0, pString.indexOf("<"));
				pubsenObject.put(keyString, dataObject.getString(keyString));
			}
			pubsenList.add(pubsenObject);

		}
		datas.put("pubsens", pubsenList);

		return datas;
	}

	/***
	 * 更新收藏、关注、阅读状态
	 * 
	 * @param user
	 * @param pubsenid
	 * @param isCareed
	 * @param isCollected
	 * @param isReaded
	 * @return
	 */
	@Bizlet("setCareedAndCollectedAndReaded")
	public Map<String, Object> setCareedAndCollectedAndReaded(String user,
			String pubsenid, String isCareed, String isCollected,
			String isReaded) {

		Map<String, Object> result = new HashMap<String, Object>();
		DataObject CfaeUserPubsen = DataObjectUtil
				.createDataObject("com.cap.cfae.cfae.CfaeUserPubsen");
		if (user != null && !"".equals(user) && pubsenid != null
				&& !pubsenid.equals("")) {
			CfaeUserPubsen.set("pubsenid", pubsenid);
			CfaeUserPubsen.set("userid", user);
			CfaeUserPubsen.set("ismyreaded", isReaded);
			CfaeUserPubsen.set("ismycollected", isCollected);
			CfaeUserPubsen.set("com.cap.cfae.cfae.CfaeUserPubsen.ismycare",
					isCareed);
			try {
				DatabaseUtil.updateEntity("default", CfaeUserPubsen);
				result.put("code", "0");
				result.put("msg", "更新成功");
			} catch (Exception e) {
				result.put("code", "1");
				result.put("msg", "更新失败");
				e.printStackTrace();
			}
		}
		return result;
	}
	} 